import tkinter as tk
from tkinter import ttk, StringVar
from tkinter import messagebox
from db import db
from datetime import datetime


class UpworkFrame(tk.Frame):
    def __init__(self, root, s, path):
        super().__init__(root)
        self.root = root

        self.id = StringVar()
        self.s = s
        self.path = path
        name = self.s + "打卡"
        if self.s == "上班":
            tit = "hhhhhhh"
        else:
            tit = "xxxxxxx"
        tk.Label(self).grid(row=0, column=1, pady=20,columnspan=2)
        tk.Label(self, text=tit, anchor="center", fg="red").grid(row=1, column=1, pady=20,columnspan=2)
        tk.Label(self, text='打卡员工号: ').grid(row=2, column=1, pady=20)
        self.e1 = tk.Entry(self, textvariable=self.id)
        self.e1.grid(row=2, column=2)
        tk.Button(self, text=name, command=self.create_upwork).grid(row=3, column=2)

    def entry_clear(self):
        self.e1.delete(0, tk.END)

    def create_upwork(self):
        name = self.path + '.json'
        db.create_json(name)
        self.id = self.e1.get()
        if self.id == "":
            messagebox.showerror("提示信息", "打卡栏工号不可为空")
            return
        db.json_read('staff.json')
        t = db.json_load
        for i in t:
            if i['id'] == self.id:
                db.json_read(name)
                time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                print(time)
                d = {'工号': i['id'], '姓名': i['name'], '部门': i['work'], '打卡时间': time}
                db.json_insert(name, d)
                tk.messagebox.showinfo("提示信息", "打卡成功")
                return
        tk.messagebox.showinfo("提示信息", "打卡失败，员工不存在")

class LeaveFrame(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        self.root = root
        self.id = tk.StringVar()
        self.name = tk.StringVar()
        self.say = tk.StringVar()
        self.yon = tk.StringVar()
        self.yon = "未批准"
        # 页面布局
        tk.Label(self).grid(row=0, column=0)
        # pady是上下边距
        tk.Label(self, text='工号: ').grid(row=1, column=1, pady=10)
        self.e1 = tk.Entry(self, textvariable=self.id)
        self.e1.grid(row=1, column=2)
        tk.Label(self, text='姓名: ').grid(row=2, column=1, pady=10)
        self.e2 = tk.Entry(self, textvariable=self.name)
        self.e2.grid(row=2, column=2)

        tk.Label(self, text='请假理由: ').grid(row=3, column=1, pady=10)
        self.e3 = tk.Entry(self, textvariable=self.say)
        self.e3.grid(row=3, column=2)


        tk.Button(self, text='提交', command=self.leave_submit).grid(row=4, column=2, sticky=tk.E)

    def entry_clear(self):
        self.e1.delete(0, tk.END)
        self.e2.delete(0, tk.END)
        self.e3.delete(0, tk.END)

    def leave_submit(self):
        self.id = self.e1.get()
        self.name = self.e2.get()
        self.say = self.e3.get()
        db.create_json('leave.json')
        if self.id == "" or self.name == "" or self.say == "":
            messagebox.showerror('提示信息', '请假条件不完整,无法上报')
            return


        db.create_json('leave.json')
        db.json_read('staff.json')

        t = db.json_load
        d = {'id': self.id, 'name': self.name, 'say': self.say, 'yon': self.yon}

        for i in t:
            if i['id'] == d['id']:
                db.json_read('leave.json')
                db.json_insert('leave.json', d)
                tk.messagebox.showinfo("提示信息", "申请成功")
                self.entry_clear()

                return
        tk.messagebox.showinfo("提示信息", "该员工不存在无法申请")

class LeaveSelectFrame(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        self.root = root
        colum_names = ['工号', '姓名', '请假理由', '是否批准']
        self.tree_view = ttk.Treeview(self, show='headings', columns=colum_names)

        # 设置表头
        for colum in colum_names:
            self.tree_view.heading(column=colum, text=colum)

        self.tree_view.tag_configure('even', background='green')  # 声明奇数行的属性
        self.tree_view.tag_configure('odd', background='white')  # 声明偶数行的属性
        # 设置每列的宽度,列中的内容居中
        self.tree_view.column(column=0, width=130, anchor='center')
        self.tree_view.column(column=1, width=130, anchor='center')
        self.tree_view.column(column=2, width=130, anchor='center')
        self.tree_view.column(column=3, width=130, anchor='center')
        self.tree_view.grid(row=0, column=0, columnspan=2)
        # self.show_clear()
        tk.Button(self, text='刷新', command=self.show_data).grid(row=3, column=1, sticky=tk.E)

   # 清空表格中的信息
    def show_clear(self):
        for item in self.tree_view.get_children():
            self.tree_view.delete(item)

    # 刷新按钮的功能
    def show_data(self):
        # 每次刷新前先清空一下表格的信息
        self.show_clear()
        #取出学生信息
        db.json_read('leave.json')
        index = 0
        for row in db.json_load:
            t = []
            my_tag = 'odd'
            if index % 2 == 0:
                my_tag = 'even'
            for key, value in row.items():
                t.append(value)
            # Treeview中无法拆解字典中的元素,因为字典或元组,是有序的可迭代集合,而字典是一种不可迭代的无序的键值对集合,通过键来访问对应的值，无法像元组和字典那样通过下标迭代访问
            self.tree_view.insert('', "end", values=t, tags=my_tag)
            index += 1